package xiaolong.lian.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xiaolong.lian.dao.DocDao;
import xiaolong.lian.dto.DocPages;
import xiaolong.lian.entity.Doc;
import xiaolong.lian.entity.Page;
import xiaolong.lian.exception.CustomException;
import xiaolong.lian.service.DocService;
import xiaolong.lian.service.PageService;
import xiaolong.lian.util.Path;

import java.io.File;
import java.util.List;
import java.util.UUID;

@Service
public class DocServiceImpl implements DocService {
    @Autowired
    private DocDao docDao;
    @Autowired
    private PageService pageService;

    @Override
    public List<Doc> getDocList(String tel) {
        return docDao.getDocsByTel(tel);
    }

    @Override
    @Transactional
    public Doc createDoc(String name, String tel) throws CustomException {
        String id = UUID.randomUUID().toString().replace("-", "");
        int row = docDao.createDoc(id, name, tel);
        if(row != 1)
            throw new CustomException("新建文档失败，请稍后重试");
        String path = Path.getAbsolutePath("/doc/" + tel);
        new File(path + "/" + id).mkdirs();
        try {
            pageService.createPage(id, 1, tel);
        }catch (CustomException e){
            throw new CustomException("初始化文档第一页失败");
        }
        return docDao.getDocById(id);
    }

    @Override
    @Transactional
    public void deleteDoc(String id, String tel) throws CustomException {
        pageService.checkDocPermission(tel, id);
        if(docDao.deleteDoc(id) != 1)
            throw new CustomException("删除文档失败，请稍后重试");
        String path = Path.getAbsolutePath("/doc/" + tel + "/" + id);
        File doc = new File(path);
        File[] pages = doc.listFiles();
        if (pages != null) {
            for (File page : pages) {
                page.delete();
            }
        }
        doc.delete();
    }

    @Override
    public void renameDoc(String id, String newName, String tel) throws CustomException {
        pageService.checkDocPermission(tel, id);
        if(docDao.renameDoc(id, newName) != 1)
            throw new CustomException("重命名失败，请稍后重试");
    }

    @Override
    @Transactional
    public DocPages syncLocalDoc(String name, List<String> pageData, String tel) throws CustomException {
        try {
            Doc doc = createDoc(name, tel);
            for (int i=1; i<pageData.size(); i++){
                pageService.createPage(doc.getDocId(), i+1, tel);
            }
            List<Page> pageList = pageService.getPageList(doc.getDocId(), tel);
            for (Page page : pageList) {
                pageService.updatePageData(page.getPageId(), pageData.get(page.getPageNum() - 1), tel);
            }
            return new DocPages(doc, pageList);
        }catch (Exception e){
            throw new CustomException("同步文档失败，请稍后重试");
        }
    }
}
